package dayong.com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;


/**
 * Servlet implementation class insertJsonpServlet
 */
@WebServlet(description = "跨域_插入数据_jsonp", urlPatterns = { "/insertJsonpServlet" })
public class insertJsonpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final String USERNAME = "1";
	private final String PASSWORD = "1";  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertJsonpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//ServletActionContext.getResponse().setHeader("Access-Control-Allow-Origin", "*");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		//String callback = request.getParameter("callback");  
		String uname = request.getParameter("uname");
		String pword = request.getParameter("pword");
		PrintWriter pw = response.getWriter();
		JSONObject jsonRes =  new JSONObject(); 
		if(USERNAME.equals(uname) && PASSWORD.equals(pword))
		{	
			jsonRes.put("text","大神,你真的666");
			jsonRes.put("text1","大神1,你真的777");
			String res= "("+jsonRes.toString()+")"; // 向前端传json
			pw.write(res);
		}else
		{
			jsonRes.put("text","sb,小菜鸡");
			String res= "("+jsonRes.toString()+")"; // 向前端传json
			pw.write(res);
			
		}
	}

}
